#include <sqlite3.h>
#include <iostream>
using namespace std;
#define BUFFER_SIZE 20
#define SIZE 64
int main(int argc, char const *argv[])
{
    // 打开sqlite3数据库
    sqlite3 *db = NULL;
    int ret = sqlite3_open("./test.db", &db);
    if (ret != SQLITE_OK)
    {
        cout << "error" << sqlite3_errmsg(db) << endl;
        exit(-1);
    }
    cout << "sqlite database open success" << endl;

#if 0
    int id;
    cout << "请输入工号" << endl;
    cin >> id;
    char name[BUFFER_SIZE] = {0};
    cout << "请输入姓名" << endl;
    cin >> name;
    int age;
    cout << "请输入年龄" << endl;
    cin >> age;
    char sex[BUFFER_SIZE] = {0};
    cout << "请输入性别" << endl;
    cin >> sex;
    float salary;
    cout << "请输入工资" << endl;
    cin >> salary;

    const char *sql = "INSERT INTO COMPANY VALUES(%d, '%s', %d, '%s', %.2f);";
    char requesSql[SIZE] = {0};
    sprintf(requesSql, sql, id, name, age, sex, salary);
    cout << "requestSql" << requesSql << endl;

    // 执行插入操作
    ret = sqlite3_exec(db, requesSql, NULL, NULL, NULL);
    if (ret != SQLITE_OK)
    {
        cout << "error" << sqlite3_errmsg(db) << endl;
        exit(-1);
    }
#endif

    const char *sql = "SELECT * FROM COMPANY";
    int row = 0;
    int colum = 0;
    char **result;
    // 查表
    sqlite3_get_table(db, sql, &result, &row, &colum, NULL);
    cout << "row: " << row << endl;
    cout << "colum: " << colum << endl;

    for (int i = 0; i < colum; i++)
    {
        printf("%-10s\t", result[i]);
    }
    printf("\n");

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < colum; j++)
        {
            printf("%-10s\t", result[(i + 1) * colum + j]);
        }
        cout << endl;
    }

    sqlite3_free_table(result);

    // 关闭数据库
    sqlite3_close(db);

    return 0;
}
